package com.wyzz.hr.action;

import java.util.Date;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpBenefitBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.Empbenefit;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.StringUtil;

public class DWRforEmpBenefit extends CompAction {

	public DWRforEmpBenefit() {
	}

	public String empBenefitAdd(Empbenefit newEbf, String empId) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("updateBenefit",
				"execute")))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		if (StringUtils.isEmpty(empId))
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee emp = empBo
				.loadEmp(empId, new String[] { "empBenefitType", "benefit",
						"config", "config.escJobgrade", "config.escEsavId" });
		if (emp == null)
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		if (emp.getBenefit() == null)
			return StringUtil.message(msgNoBenefit, new Object[] { "nobenefit",
					emp.getEmpName() });
		Empbenefit oldEbf = emp.getBenefit();
		if (newEbf.getEbfStartMonth().compareTo(oldEbf.getEbfStartMonth()) < 0)
			return msgStartYMTooEarly;
		newEbf.setEbfId(MyTools.getUUID());
		newEbf.setEmployee(emp);
		newEbf.setEbfPensionNo(oldEbf.getEbfPensionNo());
		newEbf.setEbfHousingNo(oldEbf.getEbfHousingNo());
		newEbf.setEbfMedicalNo(oldEbf.getEbfMedicalNo());
		oldEbf.setEbfEndMonth(DateUtil.yearMonthAdd(newEbf.getEbfStartMonth(),
				-1, 2));
		emp.setBenefit(newEbf);
		emp.setEmpLastChangeBy(getCurrentEmp());
		emp.setEmpLastChangeTime(new Date());
		Empsalaryconfig config = emp.getConfig();
		if (config != null) {
			config.decryEmpSalaryConf(config);
			IEmpSalaryAcctitemsBo esaitemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
					.getBean("empsalaryacctitemsBo");
			esaitemsBo.setAcctItemsConfig(new Empsalaryconfig[] { config });
			config.setEscLastChangeBy(getCurrentEmpNo());
			config.setEscLastChangeTime(new Date());
			ISalaryconfBo confBo = (ISalaryconfBo) getBean("salaryconfBo");
			confBo.interpretConfig(new Empsalaryconfig[] { config });
			config.encryEmpSalaryConf(config);
		}
		IEmpBenefitBo empbenefitBo = (IEmpBenefitBo) SpringBeanFactory
				.getBean("empbenefitBo");
		String error = empbenefitBo.insertNewBenefit(oldEbf, newEbf, emp);
		if (!"SUCC".equals(error))
			return msgSystemEx;
		else
			return StringUtil.message(msgAdjBeneSucc, new Object[] { "SUCC",
					emp.getEmpName() });
	}

	private static final long serialVersionUID = 1L;
}
